package oa.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;

import oa.entity.Attendance;
import oa.service.impl.AttendanceServiceImpl;
import oa.service.impl.UserServiceImpl;

/**
 * Servlet implementation class DeptEchart
 */
@WebServlet("/deptechart")
public class DeptEchart extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeptEchart() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");		
		PrintWriter out = response.getWriter();
		UserServiceImpl user = new UserServiceImpl();
		AttendanceServiceImpl attendan = new AttendanceServiceImpl();
		Attendance  att = new Attendance();
		Map<String, Object> maplist = new HashMap<String,Object>(2);		
		String opt = request.getParameter("opt");
		boolean flag = false;
		if("selectechart".equals(opt)) {
			try {
				int departid = Integer.parseInt(request.getParameter("departid"));		   
				int companyid = Integer.parseInt(request.getParameter("companyid"));
			    String method =   request.getParameter("method");
			    int methodyz =Integer.parseInt((method.substring(6, 7)));
			    double day = 0; //天数
			    double chuqin=0; //出勤
			    double chidao=0; //迟到
			    //判断月份的天数
			    switch (methodyz) {
				case 1:
				case 3:
				case 5:
				case 7:
				case 8:
				case 10:
				case 12:
					day=31;
					break;
				case 2:
					day=28;
					break;
				case 4:
				case 6:			
				case 9:
				case 11:
					day=30;
					break;
				default:
					day=0;
					break;
				}
			    
			    
			  //执行计算方法
			    att.setDeptId(departid);
			    att.setCompanyId(companyid);
			    java.text.DecimalFormat df=new  java.text.DecimalFormat("#0");
				double cq= attendan.selectCountList(method, att);;
			    if(cq==0.0) {
			    	chuqin=0.0;
			    	chidao = 0.0;
			    }else {
			    	flag =true;
				chuqin = cq/day*100; //得到出勤率
			    chidao = 100-chuqin;  //得到迟到率
			    }
			    maplist.put("Boolean",flag);
			    maplist.put("ChuQin", df.format(chuqin));
			    maplist.put("ChiDao", df.format(chidao));
			} catch (Exception e) {
				// TODO: handle exception
			}
			out.print(JSON.toJSONString(maplist));
		}
	    out.flush();
	    out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
